<?php


namespace app\cms\model;


use app\cms\community\lib\QueryModel;
use app\cms\dto\friendInfo;
use iflow\Response;

class friendModel extends QueryModel
{

    protected $table = "friend_table";

    public static function getFriend(string $site_title = ''): Response
    {
        $query = static::where([]);
        if ($site_title !== '') $query -> whereLike('site_title', "%{$site_title}%");
        $query = $query -> paginate([
            'page' => request() -> getParams('page'),
            'list_rows' => 10
        ]);
        if ($query -> items()) {
            return message() -> success('查询成功', $query -> items(), [
                'count' => $query -> total(),
                'page' => $query -> lastPage()
            ]);
        }
        return message() -> nodata('暂无数据');
    }

    public static function addFriend(friendInfo $friendInfo): Response
    {
        $info = (array) $friendInfo;
        unset($info['site_id']);
        return message() -> success('编辑栏目成功', $friendInfo -> site_id == 0?
            self::create($info) -> toArray():
            self::update($info, ['site_id' => $friendInfo -> site_id]) -> getChangedData()
        );
    }

    public static function updateFriend(friendInfo $friendInfo): Response
    {
        $info = (array)$friendInfo;
        unset($info['site_id']);
        $company = static::update($info, [
            'site_id' => $friendInfo -> site_id
        ]);
        if ($company) {
            return message() -> success('更新友链信息成功', $company -> toArray());
        }
        return message() -> update_failed('更新失败');
    }

    public static function deleteFriend(int $site_id = 0): Response
    {
        self::where([
            'site_id' => $site_id
        ]) -> delete();
        return message() -> success('删除友链成功');
    }
}